" File: _vimrc
" Date: 2009-09-22
" Author: gashero
" NOTE: 配置一份简单的vim配置文件
" Update: 2011-11-03
" NewAuthor: dreampuf

call pathogen#infect()
set nocompatible    "非兼容模式
syntax enable           "开启语法高亮
set background=dark "背景色
"colorscheme solarized
color desert
set ruler           "在左下角显示当前文件所在行
set showcmd         "在状态栏显示命令
set showmatch       "显示匹配的括号
set ignorecase      "大小写无关匹配
set smartcase       "只能匹配，即小写全匹配，大小写混合则严格匹配
set hlsearch        "搜索时高亮显示
set incsearch       "增量搜索
"set nohls           "搜索时随着输入立即定位，不知什么原因会关闭结果高亮
set report=0        "显示修改次数
set mouse=a         "控制台启用鼠标
set number          "行号
set nobackup        "无备份
set cursorline      "高亮当前行背景
set encoding=utf-8
set fileencoding=UTF-8
set fileencodings=ucs-bom,UTF-8,GBK,BIG5,latin1
set fileformat=unix "换行使用unix方式
set wrap
set ambiwidth=double
set noerrorbells    "不显示响铃
set visualbell      "可视化铃声
set foldmarker={,}  "缩进符号
set foldmethod=indent   "缩进作为折叠标识
set foldlevel=100   "不自动折叠
set foldopen-=search    "搜索时不打开折叠
set foldopen-=undo  "撤销时不打开折叠
set updatecount=0   "不使用交换文件
set magic           "使用正则时，除了$ . * ^以外的元字符都要加反斜线
set wildmenu        "命令模式自动补全
set wildmode=longest:full "命令模式补全方式
set autochdir       "自动设置当前文件路径为工作环境
set is

set wildignore=*.swp,*.bak,*.pyc,*.class

"缩进定义
set shiftwidth=4
set tabstop=4
set softtabstop=4
set expandtab
set smarttab
set backspace=2     "退格键可以删除任何东西
"显示TAB字符为<+++
set list
set list listchars=tab:<+

""let mapleader = ","
"映射常用操作
map [e :! erlc % <CR>
map [r :! python % <CR>
map [o :! python -i % <CR>
map [t :! rst2html.py % %<.html <CR>
map [m :! markdown % > %<.html <CR>
map [g :! open %<.html -a Google\ Chrome <CR>
"map [s :! dot -Tsvg % -o%<.svg <CR>
map [v a<C-R>=strftime("%Y-%m-%d %H:%M:%S")<CR><Esc>
map [d a<C-R>=strftime("%Y-%m-%d")<CR><Esc>
map [f :CoffeeMake<CR>
map [k :! coffeekup %<CR>
map <C-N> :tabnew .<CR>
map <C-H> :tabp<CR>
map <C-L> :tabn<CR>
map <C-C> :tabclose<CR>
map <Space> :nohl<CR>
map <C-s> :sp .<CR>
vnoremap <c-c> "+y
nmap <C-@>s :cs find s <C-R>=expand("<cword>")<CR><CR> "查找声明
nmap <C-@>g :cs find g <C-R>=expand("<cword>")<CR><CR> "查找定义
nmap <C-@>c :cs find c <C-R>=expand("<cword>")<CR><CR> "查找调用
nmap <C-@>t :cs find t <C-R>=expand("<cword>")<CR><CR> "查找指定的字符串
nmap <C-@>e :cs find e <C-R>=expand("<cword>")<CR><CR> "查找egrep模式，相当于egrep功能，但查找速度快
nmap <C-@>f :cs find f <C-R>=expand("<cfile>")<CR><CR> "查找文件
nmap <C-@>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR> "查找包含本文件的文件
nmap <C-@>d :cs find d <C-R>=expand("<cword>")<CR><CR> "查找本函数的调用的函数
:imap <C-d> <C-[>diwi

if has("gui_running")
    set lines=25
    set columns=80
    set lazyredraw  "延迟重绘
    set guioptions-=m   "不显示菜单
    set guioptions-=T   "不显示工具栏
    set guifont=Droid\ Sans\ Mono\ 9
endif

if has("autocmd")
    "回到上次文件打开所在行
    au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")
        \| exe "normal g'\"" | endif
    "自动检测文件类型，并载入相关的规则文件
    filetype plugin indent on
    filetype plugin indent off
    filetype off
    call pathogen#runtime_append_all_bundles()
    call pathogen#helptags()
    filetype indent plugin on
    "filetype on
    "filetype plugin on
    "filetype indent on
    au BufNewFile,BufRead *.pyhtml set filetype=html
    "智能缩进，使用4空格，使用全局的了
    "autocmd FileType python setlocal et | setlocal sta | setlocal sw=4
    "autocmd FileType c setlocal et | setlocal sta | setlocal sw=4
    "autocmd FileType h setlocal et | setlocal sta | setlocal sw=4
endif

"autoload _vimrc
autocmd! bufwritepost _vimrc source %
function! Short_tab()
  set shiftwidth=2
  set tabstop=2
  set softtabstop=2
endfunction
au FileType coffee :call Short_tab()
au FileType html :call Short_tab()
au FileType javascript :call Short_tab()
au FileType js :call Short_tab()


"开始折叠
set foldenable
" " 设置语法折叠
" " manual  手工定义折叠
" " indent  更多的缩进表示更高级别的折叠
" " expr    用表达式来定义折叠
" " syntax  用语法高亮来定义折叠
" " diff    对没有更改的文本进行折叠
" " marker  对文中的标志折叠
set foldmethod=indent
" "折叠相关的快捷键
" "zR 打开所有的折叠
" "za Open/Close (toggle) a folded group of lines.
" "zA Open a Closed fold or close and open fold recursively.
" "zi 全部 展开/关闭 折叠
" "zo 打开 (open) 在光标下的折叠
" "zc 关闭 (close) 在光标下的折叠
" "zC 循环关闭 (Close) 在光标下的所有折叠
" "zM 关闭所有可折叠区域
" " 设置折叠区域的宽度
set foldcolumn=0
" " 设置折叠层数为
setlocal foldlevel=1
set foldexpr=1
" " 新建的文件，刚打开的文件不折叠
" autocmd! BufNewFile,BufRead * setlocal nofoldenable


""""""""""""""""""""""""""""""
" showmarks setting
""""""""""""""""""""""""""""""
"" Enable ShowMarks
"let showmarks_enable = 1
"" Show which marks
"let showmarks_include = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"" Ignore help, quickfix, non-modifiable buffers
"let showmarks_ignore_type = "hqm"
"" Hilight lower & upper marks
"let showmarks_hlline_lower = 1
"let showmarks_hlline_upper = 1 


""""""""""""""""""""""""""
" Python mode
""""""""""""""""""""""""""
"
"call pathogen#infect()
"call pathogen#helptags()


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" cscope setting
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set cscopequickfix=s-,c-,d-,i-,t-,e-
"cscope add /Users/dreampuf/.vim/cscope.out

if has("cscope")
  set csprg=cscope
  set csto=1
  set cst
  set nocsverb
  " add any database in current directory
  if filereadable("cscope.out")
      cscope add cscope.out
  endif
  set csverb
endif

"""""""""
" Rgrep Hack for Mac
"""""""""
if has('mac')
  if system('which gxargs')
    let Grep_Xargs_Path = 'gxargs'
  else
    let Grep_Find_Use_Xargs = 0
  endif
endif

"PyDirection - Autocomplete
"let g:pydiction_location = '~/.vim/after/ftplugin/pydiction/complete-dict' 
"let g:Powerline_symbols = 'fancy'

"JSBeautify
let g:jsbeautify = {'indent_size': 2, 'indent_char': ' '}
let g:htmlbeautify = {'indent_size': 2, 'indent_char': ' ', 'max_char': 78, 'brace_style': 'expand', 'unformatted': ['a', 'sub', 'sup', 'b', 'i', 'u']}
let g:cssbeautify = {'indent_size': 2, 'indent_char': ' '}
autocmd FileType javascript noremap <buffer>  <c-w> :call JsBeautify()<cr>
autocmd FileType html noremap <buffer> <c-w> :call HtmlBeautify()<cr>
autocmd FileType css noremap <buffer> <c-w> :call CSSBeautify()<cr>

"let NERDTreeWinPos='right'
"let g:jedi#auto_initialization = 0
"Don't do stupid automatically 
let g:jedi#popup_on_dot = 0

"For column aligning
inoremap <silent> <Bar>   <Bar><Esc>:call <SID>align()<CR>a
function! s:align()
  let p = '^\s*|\s.*\s|\s*$'
  if exists(':Tabularize') && getline('.') =~# '^\s*|' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
    let column = strlen(substitute(getline('.')[0:col('.')],'[^|]','','g'))
    let position = strlen(matchstr(getline('.')[0:col('.')],'.*|\s*\zs.*'))
    Tabularize/|/l1
    normal! 0
    call search(repeat('[^|]*|',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
  endif
endfunction

"Vim
call vundle#rc()
Bundle 'gmarik/vundle'
Bundle 'godlygeek/tabular'
Bundle 'Lokaltog/vim-easymotion'
Bundle 'L9'
Bundle 'FuzzyFinder'
Bundle 'vim-coffee-script'
Bundle 'scrooloose/nerdtree'
Bundle 'taglist.vim'
Bundle 'lrvick/Conque-Shell'
Bundle 'sessionman.vim'
Bundle 'scrooloose/nerdcommenter'
Bundle 'ervandew/supertab'
Bundle 'kien/ctrlp.vim'
Bundle 'scrooloose/nerdtree'
Bundle 'mileszs/ack.vim'
Bundle 'davidhalter/jedi-vim'
Bundle 'docunext/closetag.vim'
Bundle 'maksimr/vim-jsbeautify'
Bundle 'Lokaltog/powerline'
set grepprg=ack
let g:jedi#popup_on_dot = 0
let g:closetag_html_style = 1
set completeopt=longest,menuone
"inoremap <expr> <C-n> pumvisible() ? '<C-n>' :
"  \ '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
"inoremap <expr> <M-,> pumvisible() ? '<C-n>' :
"  \ '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
"" open omni completion menu closing previous if open and opening new menu without changing the text
"inoremap <expr> <C-Space> (pumvisible() ? (col('.') > 1 ? '<Esc>i<Right>' : '<Esc>i') : '') .
"            \ '<C-x><C-o><C-r>=pumvisible() ? "\<lt>C-n>\<lt>C-p>\<lt>Down>" : ""<CR>'
"" open user completion menu closing previous if open and opening new menu without changing the text
"inoremap <expr> <S-Space> (pumvisible() ? (col('.') > 1 ? '<Esc>i<Right>' : '<Esc>i') : '') .
"            \ '<C-x><C-u><C-r>=pumvisible() ? "\<lt>C-n>\<lt>C-p>\<lt>Down>" : ""<CR>'
